package com.bs.regsystemapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.regsystemapi.dao.PatPayRecordDao;
import com.bs.regsystemapi.entity.PatPayRecord;
import com.bs.regsystemapi.entity.UserEntity;
import com.bs.regsystemapi.modal.dto.patpayrecord.GetPatPayRecordListForm;
import com.bs.regsystemapi.modal.dto.patpayrecord.QueryOrderListForm;
import com.bs.regsystemapi.modal.vo.patpayrecord.OrderListInfo;
import com.bs.regsystemapi.service.PatPayRecordService;
import com.bs.regsystemapi.service.UserService;
import com.bs.regsystemapi.utils.ManagePageResult;
import com.bs.regsystemapi.utils.RandomUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ning
 * @Date 2023/1/25 19:39
 */
@Service
public class PatPayRecordServiceImpl extends ServiceImpl<PatPayRecordDao, PatPayRecord> implements PatPayRecordService {

    @Autowired
    private PatPayRecordService patPayRecordService;

    @Autowired
    private UserService userService;

    @Override
    public ManagePageResult getPatPayRecordList(GetPatPayRecordListForm form) {
        PageHelper.startPage(form.getPageNum(), form.getPageSize());
        List<PatPayRecord> patPayRecordList = this.baseMapper.getPatPayRecordList(form);
        PageInfo<PatPayRecord> patPayRecordPageInfo = new PageInfo<>(patPayRecordList);
        return ManagePageResult.getPageResult(patPayRecordPageInfo);
    }

    @Override
    public ManagePageResult getOrderList(QueryOrderListForm form) {
        PageHelper.startPage(form.getPageNum(), form.getPageSize());
        List<OrderListInfo> orderList = this.baseMapper.getOrderList(form);
        PageInfo<OrderListInfo> orderListInfoPageInfo = new PageInfo<>(orderList);
        return ManagePageResult.getPageResult(orderListInfoPageInfo);
    }

    @Override
    public void deleteOrder(String orderNo) {
        UpdateWrapper<PatPayRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no", orderNo)
                .set("is_delete", "0");
        patPayRecordService.update(updateWrapper);
    }

    /**
     * 挂号时调用支付保存支付记录
     * @param patNo
     * @param personNo
     * @param doctorNo
     * @param regNo
     */
    @Override
    public void savePay(String patNo, String personNo, String doctorNo, String regNo) {
        PatPayRecord payRecord = new PatPayRecord();

        String time = Long.toString(System.currentTimeMillis());
        time = time.substring(0, time.length()-3);
        String orderNo = RandomUtils.getrandom(8) + time;

        UserEntity userInfo = userService.getUserInfo(doctorNo);

        payRecord.setPatNo(patNo);
        payRecord.setPersonNo(personNo);
        payRecord.setDoctorNo(doctorNo);
        payRecord.setRegNo(regNo);
        payRecord.setOrderNo(orderNo);
        payRecord.setOrderState("1");
        payRecord.setUserCost(Double.parseDouble(userInfo.getReservePrice()));
        payRecord.setPayTime(new Date());
        payRecord.setPayType("微信支付");
        payRecord.setPayMode("在线支付");
        payRecord.setCreatTime(new Date());
        payRecord.setFinishTime(new Date());

        this.save(payRecord);
    }
}
